package com.example.listview2;

import java.util.ArrayList;

import com.example.Service.NewsService;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity extends Activity {
	private ListView lv1;
	private ArrayList<NewsService> news=new ArrayList<NewsService>();
	myAdapter adapter; 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        lv1=(ListView) findViewById(R.id.listview1);
        	
        initData();
        adapter=new myAdapter();
        lv1.setAdapter(adapter);
    }
    int index=1;
    void initData()
    {
    	for(int i=0;i<30;i++)
    	{
    		NewsService n=new NewsService();
    		n.title="title-"+i;
    		n.content="content-"+i;
    		index++;
    		news.add(n);
    	}
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
    class myAdapter extends BaseAdapter
    {

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return news.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return news.get(arg0);
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return arg0;
		}

		@Override
		public View getView(int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			viewHolder vh=null;
			if(arg1==null)
			{
				arg1=getLayoutInflater().inflate(R.layout.listview, null);
				vh=new viewHolder();
				vh.tvTile=(TextView) arg1.findViewById(R.id.textView1);
				vh.tvContent=(TextView) arg1.findViewById(R.id.textView2);
				arg1.setTag(vh);
			}else{
				vh=(viewHolder) arg1.getTag();
			}
			NewsService n=news.get(arg0);
			vh.tvTile.setText(n.title);
			vh.tvContent.setText(n.content);
			return arg1;
		}
    	
    }
    static class viewHolder
    {
    	TextView tvTile,tvContent;
    }
}
